package com.wq.article.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wq.article.entity.Article;
import com.wq.article.service.ArticleService;
import com.wq.common.entity.PageResult;
import com.wq.common.entity.Result;
import com.wq.common.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("article")
@CrossOrigin
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @PutMapping("check/{id}")
    public Result checkById(@RequestParam(value = "state",defaultValue = "1") String state,@PathVariable String id){
    articleService.checkById(state,id);
    return new Result(true, StatusCode.OK,"校验成功");
    }

    @PostMapping("search/{page}/{size}")
    public Result search(@RequestBody Map map,@PathVariable int page,@PathVariable int size){
        Page page1=articleService.search(map, page, size);
        return new Result(true,StatusCode.OK,"查询成功",new PageResult<Article>(page1.getTotal(),page1.getRecords()));
    }
    @PostMapping
    public Result add(@RequestBody Article article) {
        return new Result(true, StatusCode.OK, "增加成功");
    }
    /** * 修 改 文 章
     *
     * @param article * @return */
    @PutMapping(value = "{id}")
    public Result update(@RequestBody Article article, @PathVariable String id) {
        article.setId(id); articleService.update(article);
        return new Result(true, StatusCode.OK, "修改成功");
    }
    /** * 删 除 文 章
     *
     * @param id * @return */
    @DeleteMapping(value = "{id}")
    public Result deleteById(@PathVariable String id) {
        articleService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }
    @GetMapping
    public Result findAll(){
        List<Article> all = articleService.findAll();
        return new Result(true,StatusCode.OK,"查询所有",all);
    }



}
